﻿<html xmlns="http://www.w3.org/1999/xhtml" class="hb-loaded">
 <head>
  <style>
     #SCity,#SProvince,#SDistrict,#SName,#SPhone,#SStreet,#shou{
      color: black;
     }
   #province2,#district2,#city2{
    width: 80px;
   }
  </style>
 </head>
 <body>
 <link href="css/shopping.css?v=1.3.5" type="text/css" rel="stylesheet" />
  <script src="../js/jquery.js" type="text/javascript"></script>
  <script src="../js/shoppcart.js" type="text/javascript"></script>

  <title>购物车 - 提交订单</title> 
  <iframe src="javascript:false;" style="display: none;"></iframe> 
  <form id="aspnetForm" action="Address.aspx?action=post&amp;process=darryring" method="post" name="aspnetForm"> 
   <div> 
    <input type="hidden" value="/wEPDwULLTEyMjU5Nzc3NTgPZBYCZg9kFgICAxBkZBYGZg8PFgIeBFRleHQFD+S9oOWlve+8gUtMTmdPa2RkAgEPFgIeBWNsYXNzBRRzaG9wX25hdiBzaG9wX25hdi1kZGQCAg9kFgRmDxYCHgtfIUl0ZW1Db3VudAIBFgJmD2QWAmYPFQkFNjE5MjESIGNoZWNrZWQ9ImNoZWNrZWQiG+emj+W7uuecgem+meWyqeW4gumVv+axgOWOvxLlqIPlqIPkurLliLDpnZLlspsG5byg5ZCICzEzODUxNDM1NTkzDOm7mOiupOWcsOWdgAU2MTkyMQU2MTkyMWQCAQ8WAh8CAgEWAmYPZBYCZg8VBRdGb3JldmVy57O75YiXIOe7j+WFuOasvgkxOEvnmb3ph5EBNxHntKDmnZDngasmaGVhcnRzOwYxMCw4MDBkZF0UgGn0DxQ3QCVr+e2vHjvosTt8" id="__VIEWSTATE" name="__VIEWSTATE" /> 
   </div> 
   <div> 
    <input type="hidden" value="8C541494" id="__VIEWSTATEGENERATOR" name="__VIEWSTATEGENERATOR" /> 
   </div> 
   <!--全部背景--> 
   <div class="all-thing"> 
    <!--中间内容--> 
    <div class="tcmain"> 
     <!--头部--> 
     <div class="shop_top"> 
      <div class="shopt_left fl"> 
       <a title="Darry Ring" href="../home/home.html"> <img width="186" height="42" src="../images/dr.png" /> </a>
       <span>求婚钻戒领导品牌</span> 
      </div> 
      <div class="shopt_right fr">
       <span id="ctl00_ltlUname"></span>
       <a class="my_dr" href="../member/myDr.html">我的DR</a>
       <a onClick="logout()" class="tc_dr" href="javascript:void(0)">退出</a>
       <a class="help_dr" href="../help.html">帮助中心</a>
      </div> 
     </div> 
     <!--头部end--> 
     <!--导航条--> 
     <div class="shop_nav shop_nav-dd" id="ctl00_showNav"></div> 
     <!--导航条end-->
     <script type="text/javascript">
      let userName;
      let userId;

      $.getJSON("http://localhost:8080/DiamondRing/UserName",function (date) {
       userId=date.userId;
       userName=date.loginName;
          init();
          findAddress();
       if (userName.length>0){
        $("#ctl00_ltlUname").html("您好!"+userName);
       }
      })
$(function () {
Shu();
Zjie();
});
      let isDefault;
      let addressid;
      function findAddress() {
       $.getJSON("http://localhost:8080/DiamondRing/AddressFindServlet","userid="+userId,function (date) {
        $.each(date,function () {
            addressid=this.addressId;
            let userid=this.userId;
            let addName=this.addName;
            $("#SName").html(addName)
            let diamondProvince=this.diamondProvince;
            $("#SProvince").html(diamondProvince)
            let diamondCity=this.diamondCity;
            $("#SCity").html(diamondCity)
            let diamondDistrict=this.diamondDistrict;
            $("#SDistrict").html(diamondDistrict)
            let diamondStreet=this.diamondStreet;
            $("#SStreet").html(diamondStreet)
            let addPhone=this.addPhone;
            $("#SPhone").html(addPhone)
            isDefault=this.isDefault;
           })

       })
      }

      function init() {
       $.getJSON("http://localhost:8080/DiamondRing/ShoppingServlet","userid="+userId,function (date) {
        var leetr;
        $.each(date,function () {
         if (this.lettering==null){
          leetr="";
         }else {
          leetr=this.lettering;
         }
               $("#order").append(" <tr class=\"shop_adressqr-sec\"> \n" +
                       "            <td class=\"shop_adress-shoop\">"+this.goods.series+'\t'+this.goods.style+"</td> \n" +
                       "            <td class=\"shop_adress-cz\">"+this.goods.texture+"</td> \n" +
                       "            <td class=\"shop_adress-sc\">"+this.size+"</td> \n" +
                       "            <td class=\"shop_adress-kz\">"+leetr+"</td> \n" +
                       "            <td class=\"shop_adress-pirce\"><span style=\"font-family:微软雅黑\" >￥<input name='pirs' value='"+formatPrice(this.goods.diamondPrice)+"' disabled style='background-color:#fff;border:none;height: 20px;width: 70px' ></span></td> \n" +
                       "           </tr>")
         Shu();
         Zjie();
           })
       })
      }

      //计算商品数量
      function Shu(){
       $("#shu").html($("#order").children().length-1)
      }

      //总价格
      function Zjie() {
       var prices=document.getElementsByName("pirs");
       var sum=0;
       for(var i=0; i<prices.length;i++){
        sum+=parseInt(formatPrice2(prices[i].value));
       }
       document.getElementById("price").innerHTML=formatPrice(sum);
      }

      //为价格添加逗号
      function formatPrice(price) {
       return String(price).replace(/\B(?=(\d{3})+(?!\d))/g, ',');
      }

      //为价格去掉逗号
      function formatPrice2(price) {
       return String(price).replace(/[^0-9]*/g,"");
      }

        function addressItemSelected(control) {
            $(control).addClass("check_bk").siblings().removeClass("check_bk");
            $(control).find("input:radio").attr("checked", true);
        }


        //市数据加载事件
        var CityDataLoadEvent = function () { };
        var DistrictDataLoadEvent = function () { };

        $(function () {
            //绑定地址点击事件
            $(".shop_adress-top:not(:last)").each(function () {
                $(this).click(function () {
                    hideNewAddress();
                    addressItemSelected(this);
                });
            });

            //绑定新增地址事件
            $(".shop_adress-top:last").click(function () {
                //地址标题，新增还是修改
                $(".shop_adress-add h4").text("新增新地址");
                $("#province").show()
                $("#province2").hide()
                $("#city").show()
                $("#city2").hide()
                $("#district").show()
                $("#district2").hide()
                $("#bt2").hide()
                $("#bt1").show()
                addressItemSelected(this);
                showNewAddress();
            });

            //省下拉框ID
            var province = "#province";
            var city = "#city";
            var district = "#district";

            //加载省信息

            //省下拉框onchange事件
          /*  $(province).change(function () {
                var dataLoad = function (data) {
                    $(city + " option:not(:first)").remove();
                    $(data).each(function () {
                        bindDdlData(city, this);
                    });
                    CityDataLoadEvent();
                };

            });*/

            //市下拉框onchange事件
           /* $(city).change(function () {
                var dataLoad = function (data) {
                    $(district + " option:not(:first)").remove();
                    $(data).each(function () {
                        bindDdlData(district, this);
                    });
                    DistrictDataLoadEvent();
                };

            });*/





        });

        //修改的验证方法
     function updateAdd() {
      var data = getData();

      if (data.name == "") {
       alert("收货人不能为空。");
       return false;
      }
      if ($("#province2").val() == "" &&$("#province2").val() == null) {
       alert("省份不能为空。");
       return false;
      }
      if ($("#city2").val() == "" && $("#city2").val() == null) {
       alert("城市不能为空。");
       return false;
      }

      if ($("#district2").val() == ""&& $("#district2").val() == null) {
       alert("区县不能为空。");
       return false;
      }
      if (data.street == "") {
       alert("详细地址不能为空。");
       return false;
      }
      if (data.mobile == "") {
       alert("手机号");
       return false;
      }else if (!phone.test(data.mobile)){
         alert("手机号格式不正确")
         return false;
      }
      var action = "save";
      if (isNaN(data.ID)) {
       action = "add";
      }
      return true;
}
var phone=/^1(3\d|4[5-9]|5[0-35-9]|6[567]|7[0-8]|8\d|9[0-35-9])\d{8}$/;
       //新增的验证方法
        function NotAdd() {
         var data = getData();

         if (data.name == "") {
          alert("收货人不能为空。");
          return false;
         }
         if (data.province == "") {
          alert("省份不能为空。");
          return false;
         }
         if (data.city == "") {
          alert("城市不能为空。");
          return false;
         }
         if (data.district == "") {
          alert("区县不能为空。");
          return false;
         }
         if (data.street == "") {
          alert("详细地址不能为空。");
          return false;
         }
         if (data.mobile == "") {
          alert("手机号不能为空");
          return false;
         }else if (!phone.test(data.mobile)){
          alert("手机号格式不正确")
          return false;
         }

         var action = "save";
         if (isNaN(data.ID)) {
          action = "add";
         }
         return true;
        }

        //修改默认地址
      function UpdateAdd() {
          let addName=$("#addressName").val()
          let province=$("#province2").val()
          let city=$("#city2").val()
          let district=$("#district2").val()
          let street=$("#street").val()
          let mobile=$("#mobile").val()
       if (updateAdd()){
        $.get("http://localhost:8080/DiamondRing/AddressUpdateServlet","userid="+userId+"&addName="+addName+"&province="+province+"&city="+city+"&district="+district+"&street="+street+"&mobile="+mobile+"&addressid="+addressid,function (date) {
         if (date>0){
          alert("默认地址修改成功");
          $("#SProvince").html(province)
          $("#SCity").html(city)
          $("#SDistrict").html(district)
          $("#SStreet").html(street)
          $("#SName").html(addName)
          $("#SPhone").html(mobile)
         }else {
          alert("失误操作");
         }
        })
       }


      }

      //新增地址
      function InsertAdd(){
         let isDefault=0;
       let addName=$("#addressName").val()
       let province=$("#province").val()
       let city=$("#city").val()
       let district=$("#district").val()
       let street=$("#street").val()
       let mobile=$("#mobile").val()
         if ($('#cbDefaultAddress').attr('checked')) {
           isDefault=1;
          if ( NotAdd()){
           $.get("http://localhost:8080/DiamondRing/AddressInsertServlet","userid="+userId+"&addName="+addName+"&province="+province+"&city="+city+"&district="+district+"&street="+street+"&mobile="+mobile+"&isDefault="+isDefault,function (date) {
            if (date>0){
             alert("新默认地址添加成功");
             $("#SProvince").html(province)
             $("#SCity").html(city)
             $("#SDistrict").html(district)
             $("#SStreet").html(street)
             $("#SName").html(addName)
             $("#SPhone").html(mobile)
             $("#shou").html("(收)")
            }else {
             alert("失误操作");
            }
           })
          }
         }else {
           if ( NotAdd()){
            $.get("http://localhost:8080/DiamondRing/AddressInsertServlet","userid="+userId+"&addName="+addName+"&province="+province+"&city="+city+"&district="+district+"&street="+street+"&mobile="+mobile+"&isDefault="+isDefault,function (date) {
                 if (date>0){
                    alert("新地址添加成功");
                 }else {
                    alert("失误操作");
                 }
            })
           }

         }


      }

        function bindDdlData(cid,data) {
            $(cid).append($("<option value=\"" + data.code + "\">" + data.name + "</option>"));
        }

        //修改地址初始值
        function showAddress(id) {
            //地址标题，新增还是修改
            $(".shop_adress-add h4").text("修改地址");
            $("#province").hide()
            $("#province2").show().val($("#SProvince").html())
            $("#city").hide()
            $("#city2").show().val($("#SCity").html())
            $("#district").hide()
            $("#district2").show().val($("#SDistrict").html())
            $("#addressName").val($("#SName").html())
            $("#street").val($("#SStreet").html())
            $("#mobile").val($("#SPhone").html())
         if (isDefault==1){
            $("#cbDefaultAddress").attr("checked", true);
         }
            $(".shop_adress-add").show();
            $(".shop_adress-add").attr("id","address_"+id);
            $("#bt1").hide();
            $("#bt2").show();


        }

        function showNewAddress() {
            resetNewAddress();
            $(".shop_adress-add").show();
        }

        function hideNewAddress() {
            resetNewAddress();
            $(".shop_adress-add").hide();
        }

        //清空输入框的值
        function resetNewAddress() {
            $("#addressName").val("");
            $("#province").val(provinceArr[0]);
            /*$("#city").val(cityArr[0][0])
            $("#district").val(countyArr[0][0][0])*/
            $("#street").val("");
            $("#postcode").val("");
            $("#mobile").val("");
            $("#cbDefaultAddress").attr("checked", false);

            $("#city option:not(:first)").remove();
            $("#district option:not(:first)").remove();
        }

        //拿到输入的值是否为空
        function getData() {
            var id = $(".shop_adress-add").attr("id");
            id = id ? id.replace("address_", "") : undefined;
            return {
            "ID":id,
            "name":$("#addressName").val(),
            "province": $("#province").val() == "请选择" ? "" : $("#province").text(),
            "city": $("#city").val() == "请选择" ? "": $("#city").text(),
            "district": $("#district").val() == "请选择" ? "" : $("#district").text(),
            "street":$("#street").val(),
            "mobile":$("#mobile").val(),
            "IsDefault":$("#cbDefaultAddress").attr("checked")};
        }

        function setAddress(data) {
            $("#addressName").val(data.name);
            $("#province option").each(function () {
                if (data.city.indexOf($(this).text()) != -1) {
                    $("#province").val($(this).val());
                    $("#province").change();
                }
            });
            CityDataLoadEvent = function () {
                $("#city option").each(function () {
                    if (data.city.indexOf($(this).text()) != -1) {
                        $("#city").val($(this).val());
                        $("#city").change();
                    }
                });
            };
            DistrictDataLoadEvent = function () {
                $("#district option").each(function () {
                    if (data.city.indexOf($(this).text()) != -1) {
                        $("#district").val($(this).val());
                    }
                });
            };

            $("#street").val(data.street);
            $("#postcode").val(data.code);
            $("#mobile").val(data.mobile);
            $("#telephone").val(data.phone);
            $("#cbDefaultAddress").attr("checked", data.IsDefault);
        }

        //删除默认地址
        function deleteAddress(id) {
            if (confirm("确认是否删除？")) {
               $.get("http://localhost:8080/DiamondRing/AddressDeleteServlet","userid="+id+"&addressid="+addressid,function (date) {
                     if (date>0){
                      alert("地址删除成功，请添加新地址");
                      $("#SProvince").html("")
                      $("#SCity").html("")
                      $("#SDistrict").html("")
                      $("#SStreet").html("")
                      $("#SName").html("")
                      $("#SPhone").html("")
                      $("#shou").html("")
                     }else {
                      alert("无效操作")
                     }
               })
            }
        }

        //判断是否选中地址
        function submitOrder() {
            //check
             if ($("#SName").html()==0){
             alert("请添加一个地址。");
             return;
            }
            if ($(".shop_adress-top:not(:last) :radio:checked").length == 0) {
                alert("请先选择一个地址。");
                return;
            }
            orderAdd();
            var nu = '1';



        }

        $(function () {
            $("#aspnetForm").attr("action", "submitOrders.html");
        });

      function orderId() {
       var vNow = new Date();
       var sNow = "";
       sNow += String(vNow.getFullYear());
       sNow += String(vNow.getMonth() + 1);
       sNow += String(vNow.getDate());
       sNow += String(vNow.getHours());
       sNow += String(vNow.getMinutes());
       sNow += String(vNow.getSeconds());
       sNow += String(vNow.getMilliseconds());
       return sNow;
      }
      function dateCart() {
       let vNow = new Date();
       let sNow = "";
       sNow += String(vNow.getFullYear()+"-");
       sNow += String(vNow.getMonth() + 1+"-");
       sNow += String(vNow.getDate());
        return sNow;
      }
        function orderAdd() {
           let shoppingNum=$("#shu").html()
           let transactionPrice=formatPrice2($("#price").html())
           let remarks=$("#remarks").val()
           let orderNumber=orderId();
           let purchaseTime=dateCart();
         $.getJSON("http://localhost:8080/DiamondRing/OrderInsertServlet","addressid="+addressid+"&userid="+userId+"&orderNumber="+orderNumber+"&purchaseTime="+purchaseTime+"&transactionPrice="+transactionPrice+"&shoppingNum="+shoppingNum+"&remarks="+remarks,function (date) {
          if (date>0){
                $.getJSON("http://localhost:8080/DiamondRing/OrderGoodsInsertServlet","addressid="+addressid+"&userid="+userId+"&orderNumber="+orderNumber,function (date) {
                 if (date>0){
                          alert("已加入订单")
                           $.get("http://localhost:8080/DiamondRing/EmptyServlet","date="+userId,function (date) {
                            if (date>0){
                             location.href="http://localhost:8080/DiamondRing/%20shoppingCart/submitOrders.html?orderNumber="+orderNumber+"&price="+transactionPrice;
                            }
                           });
                       }else {
                          alert("订单商品无法生成")
                       }
                })
          }else {
           alert("订单无法生成")
          }
         })
        }

    </script>
     <!--内容--> 
     <div class="shop_cort"> 
      <!--左边--> 
      <div class="shop_cort-left fl"> 
       <h3>收货人信息</h3> 
       <!--填写地址信息--> 
       <div class="shop_cort-adress"> 
        <!--地址--> 
        <div class="shop_adress-top"> 
         <input type="radio" checked="checked" name="adress" value="61921" />
         <label><a id="SProvince"></a> <a id="SCity"></a> <a id="SDistrict"></a> <a id="SStreet"></a></label>
         <label>&nbsp; <a id="SName"></a><a id="shou">(收)</a> <a id="SPhone"></a></label>
         <span>默认地址</span>
         <a href="javascript:showAddress(userId);">修改</a>
         <i>|</i>
         <a href="javascript:deleteAddress(userId);">删除</a>
        </div> 
        <div class="shop_adress-top"> 
         <input type="radio" id="addressNew" name="adress" />
         <label class="adress_color" for="addressNew">添加新地址</label> 
        </div> 
        <!--地址end--> 
        <!--新加地址--> 
        <div style="display:none;" class="shop_adress-add"> 
         <h4>添加新地址:</h4> 
         <div class="shop_adress-Toadd"> 
          <label><i>*</i>收 货 人：</label> 
          <input type="text" class="true_name" placeholder="请准确填写真实姓名" id="addressName" /> 
         </div> 
         <div class="shop_adress-Toadd"> 
          <label><i>*</i>所在地区：</label>
          <span>省：</span>
          <select name="province" id="province">

          </select>
          <input style="display: none" type="text" name="province2" id="province2" >
          <span>市：</span>
          <select name="city" id="city">

          </select>
          <input style="display: none" type="text" name="city2" id="city2" >
          <span>县：</span>
          <select name="district" id="district">

          </select>
          <input style="display: none" type="text" name="district2" id="district2" >
         </div>
         <div class="shop_adress-Toadd">
          <label><i>*</i>详细地址：</label> 
          <input  type="text" placeholder="请填写详细路名及门牌号" class="adt_1" id="street" />
         </div>
         <div class="shop_adress-Toadd"> 
          <label><i>*</i>手机号码：</label> 
          <input type="text" id="mobile" />
         </div> 
         <div class="shop_adress-sp"> 
          <input type="checkbox" id="cbDefaultAddress" /> 
          <label for="cbDefaultAddress" >设为默认地址</label>
         </div> 
         <div class="shop_adress-save">
          <div class="bt1" id="bt1" onclick="InsertAdd();">
           <span class="save">保存收货人信息</span>
          </div>

          <div class="bt1" id="bt2" onclick="UpdateAdd();">
           <span class="save"  >保存收货人信息</span>
          </div>
         </div> 
        </div> 
        <!--新加地址end--> 
        <div class="shop_adress-qr"> 
         <div class="shop_adressqr-top"> 
          <a class="fr" href="shoppingcart.html">返回修改购物车&gt;&gt;</a>
          <span>确认订单信息</span> 
          <i>提示：专属订制产品，需要相关证书认证时间。建议尽快提交。</i> 
         </div> 
         <!--订单--> 
         <table cellspacing="0" cellpadding="0" border="0" class="shop_adressqr-of"> 
          <tbody id="order">
           <tr class="shop_adressqr-first"> 
            <td class="shop_adress-shoop">商 品</td> 
            <td class="shop_adress-cz">材 质</td> 
            <td class="shop_adress-sc">手寸/尺寸</td> 
            <td class="shop_adress-kz">刻 字</td> 
            <td class="shop_adress-pirce">价 格</td> 
           </tr>
          </tbody>
         </table> 
         <!--订单end--> 
         <!--总计--> 
         <div class="shop_adress-zj"> 
          <div class="fl"> 
           <span>总计</span> 
          </div> 
          <div class="fr"> 
           <i id="shu">1</i>
           <span>件商品</span> 
           <span>应付金额：</span> 
           <i style="font-family:微软雅黑" class="fw_bold">￥<span id="price"></span></i>
          </div> 
         </div> 
         <!--总计--> 
         <!--最后一块--> 
         <div class="shop_adress-last"> 
          <div class="shop_adress-ddbz fl"> 
           <p>订单备注</p> 
           <textarea placeholder="此处请勿填写有关支付方面的信息,留言请在50字以内。" id="remarks" class="shop_adress-text" name="ordernote"></textarea>
          </div> 
          <div onClick="submitOrder();" class="shop_adress-tjdd fr"> 
           <div class="bt1 fr">
            <span>立即提交订单</span>
           </div> 
          </div> 
         </div> 
         <!--最后一块end--> 
        </div> 
       </div> 
       <!--填写地址信息end--> 
      </div> 
      <!--左边end--> 
      <!--右边--> 
      <!--右边--> 
      <div class="shop_cort-right fr"> 
       <div class="shop_right-nr"> 
        <h3>购物帮助指南</h3> 
        <div class="shop_right-zx line_bottom"> 
         <p class="shop_lx">24小时在线客服</p> 
         <p class="shop_tel">400-01-13520</p> 
        </div> 
        <div class="shop_right-zf line_bottom"> 
         <h4>支付安全保障</h4> 
         <p>安全支付系统采用SSL加密。</p> 
         <ul class="shop_right-ul"> 
          <li class="shop_cor-yl"></li> 
          <li class="shop_cor-cft"></li> 
          <li class="shop_cor-zf"></li> 
         </ul> 
        </div> 
        <div class="shop_right-ps"> 
         <h4>全球配送</h4> 
         <p class="shop_kd">支持全球配送,店铺取货</p> 
         <p class="shop_bj">全程保价 无风险</p> 
        </div> 
       </div> 
      </div> 
      <!--右边end--> 
      <!--右边end--> 
     </div> 
     <!--内容end--> 
     <!--底部--> 
     <div class="cmain shop_foot"> 
      <p>Copyright &copy; 2006-2015  戴瑞珠宝 All Rights Reserved. 粤ICP备XXX号</p> 
      <p>中国互联网违法信息举报中心 | 中国公安网络110报警服务 | 本网站提供所售商品的正式发票</p> 
      <div class="shop_foot-img"> 
       <img width="776" height="55" src="../home/image/db.jpg" />
      </div> 
     </div> 
     <!--底部end--> 
    </div> 
    <!--全部背景end--> 
   </div>


   <script type="text/javascript">
        function logout() {
            if (window.confirm('确定退出吗？')) {
             //用户退出
             $.get("http://localhost:8080/DiamondRing/OutServlet",function (data) {
              if (data>0){
               window.location.href = "http://localhost:8080/DiamondRing/home/home.html";
              }

             });
            }
        }
</script> 
  </form>
 <script src="js/city.js" ></script>
 <script src="js/cascading.js" ></script>
 </body>
</html>